package org.example.audio;

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

import java.net.URL;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AudioPlayer {

private MediaPlayer mediaPlayer;
private final ExecutorService executor;

public AudioPlayer() {
    executor = Executors.newCachedThreadPool();
}

public void playSound() {
    CompletableFuture.runAsync(() -> {
        try {
            URL resourceUrl1 = getClass().getClassLoader().getResource("mp3/WoodBlock.mp3");
            if (resourceUrl1 != null) {
                mediaPlayer = new MediaPlayer(new Media(resourceUrl1.toExternalForm()));
                mediaPlayer.play();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }, executor);
}

public void stopSound() {
    if (mediaPlayer != null) {
        mediaPlayer.stop();
    }
    executor.shutdownNow();
}
}